package com.hie2j.ad1016;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class EditActivity extends AppCompatActivity {
    private EditText edtName;
    private EditText edtAge;
    private EditText edtNo;
    private RadioButton radio1;
    private RadioButton radio2;
    private RadioButton radio3;
    private RadioButton radio4;
    private RadioButton radio5;
    private Button btnSaveStu;
    private String no;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        setTitle("编辑学生");

        findViews();

        initElement();

        btnOnclick();

    }
    /*
    *   初始化所有元素
    * */
    private void initElement() {
        Intent intent = getIntent();
        String name = intent.getStringExtra("NAME");
        int age = intent.getIntExtra("AGE",0);
        int imgId = intent.getIntExtra("IMG", R.drawable.dog1);
        no = intent.getStringExtra("NO");

        edtName.setText(name);
        edtAge.setText(String.valueOf(age));
        edtNo.setText(no);

        switch(imgId){
            case R.drawable.dog1:
                radio1.setChecked(true);
                break;
            case R.drawable.dog2:
                radio2.setChecked(true);
                break;
            case R.drawable.dog3:
                radio3.setChecked(true);
                break;
            case R.drawable.dog4:
                radio4.setChecked(true);
                break;
            case R.drawable.dog5:
                radio5.setChecked(true);
                break;
        }
    }

    /*
    *   按钮点击事件
    * */
    private void btnOnclick() {
        btnSaveStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = edtName.getText().toString();
                int age = Integer.parseInt(edtAge.getText().toString());
                String no  = edtNo.getText().toString();

                int imgId = R.drawable.dog1;
                if (radio1.isChecked()){
                    imgId = R.drawable.dog1;
                }else if (radio2.isChecked()){
                    imgId = R.drawable.dog2;
                }else if (radio3.isChecked()){
                    imgId = R.drawable.dog3;
                }else if (radio4.isChecked()){
                    imgId = R.drawable.dog4;
                }else if (radio5.isChecked()){
                    imgId = R.drawable.dog5;
                }

                Intent intent = new Intent();
                intent.putExtra("NAME",name);
                intent.putExtra("AGE",age);
                intent.putExtra("IMG",imgId);
                intent.putExtra("NO",no);
                setResult(3003,intent);

                finish();
            }
        });
    }

    /*
    *   绑定所有组件
    * */
    private void findViews() {
        edtName = findViewById(R.id.stu_edtName);
        edtAge = findViewById(R.id.stu_edtAge);
        edtNo = findViewById(R.id.stu_edtNo);
        radio1 = findViewById(R.id.radio1);
        radio2 = findViewById(R.id.radio2);
        radio3 = findViewById(R.id.radio3);
        radio4 = findViewById(R.id.radio4);
        radio5 = findViewById(R.id.radio5);
        btnSaveStu = findViewById(R.id.btn_saveStu);
    }
}
